[cargo new] Add a new --vcs enumeration flag
authorWesley Wiser <wwiser@gmail.com>
Sun, 4 Jan 2015 23:11:55 +0000 (18:11 -0500)
committerWesley Wiser <wwiser@gmail.com>
Tue, 6 Jan 2015 01:33:47 +0000 (20:33 -0500)
Replace the --git, --hg, and --no-git flags with a --vcs enumeration
flag which supports the values "git", "hg", and "none".

Related to #1116

src/bin/new.rs
src/doc/guide.md
tests/test_cargo_new.rs

index 80f6c5999f96dd4a94820ea1f10bc0532966cc93..2fb43478cfde4fa105a02dfd43bc9e53cbf9f91c 100644 (file)
@@ -1,18 +1,34 @@
 use std::os;
+use rustc_serialize::{Decodable, Decoder};
 
 use cargo::ops;
 use cargo::core::MultiShell;
 use cargo::util::{CliResult, CliError};
 
+#[deriving(Show, PartialEq)]
+enum VersionControl { Git, Hg, NoVcs }
+
+impl<E, D: Decoder<E>> Decodable<D, E> for VersionControl {
+    fn decode(d: &mut D) -> Result<VersionControl, E> {
+        Ok(match try!(d.read_str()).as_slice() {
+            "git" => VersionControl::Git,
+            "hg" => VersionControl::Hg,
+            "none" => VersionControl::NoVcs,
+            n => {
+                let err = format!("could not decode '{}' as version control", n);
+                return Err(d.error(err.as_slice()));
+            }
+        })
+    }
+}
+
 #[deriving(RustcDecodable)]
 struct Options {
     flag_verbose: bool,
     flag_bin: bool,
     flag_travis: bool,
-    flag_hg: bool,
-    flag_git: bool,
-    flag_no_git: bool,
     arg_path: String,
+    flag_vcs: Option<VersionControl>,
 }
 
 pub const USAGE: &'static str = "
@@ -24,10 +40,9 @@ Usage:
 
 Options:
     -h, --help          Print this message
-    --no-git            Don't initialize a new git repository
-    --git               Initialize a new git repository, overriding a
-                        global `git = false` configuration
-    --hg                Initialize a new hg repository
+    --vcs <vcs>         Initialize a new repository for the given version
+                        control system (git or hg) or do not initialize any version 
+                        control at all (none) overriding a global configuration. 
     --travis            Create a .travis.yml file
     --bin               Use a binary instead of a library template
     -v, --verbose       Use verbose output
@@ -37,13 +52,12 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>
     debug!("executing; cmd=cargo-new; args={}", os::args());
     shell.set_verbose(options.flag_verbose);
 
-    let Options { flag_no_git, flag_travis,
-                  flag_bin,arg_path, flag_git, flag_hg, .. } = options;
+    let Options { flag_travis, flag_bin, arg_path, flag_vcs, .. } = options;
 
     let opts = ops::NewOptions {
-        no_git: flag_no_git,
-        git: flag_git,
-        hg: flag_hg,
+        no_git: flag_vcs == Some(VersionControl::NoVcs),
+        git: flag_vcs == Some(VersionControl::Git),
+        hg: flag_vcs == Some(VersionControl::Hg),
         travis: flag_travis,
         path: arg_path.as_slice(),
         bin: flag_bin,
index a8788af03a87805db2bc06584b7105132cc63bb0..43994734df796420d98c4fbc2413a33365d89420 100644 (file)
@@ -33,7 +33,7 @@ $ cargo new hello_world --bin
 
 We're passing `--bin` because we're making a binary program: if we
 were making a library, we'd leave it off. If you'd like to not initialize a new
-git repository as well (the default), you can also pass `--no-git`.
+git repository as well (the default), you can also pass `--vcs none`.
 
 Let's check out what Cargo has generated for us:
 
index 9906b8342431e181b4761ce5a5d6bd16fc8da048..49b995817a8e4bfcabcf420a82a6b48d18c1ee54 100644 (file)
@@ -24,7 +24,7 @@ fn cargo_process(s: &str) -> ProcessBuilder {
 
 test!(simple_lib {
     os::setenv("USER", "foo");
-    assert_that(cargo_process("new").arg("foo").arg("--no-git"),
+    assert_that(cargo_process("new").arg("foo").arg("--vcs").arg("none"),
                 execs().with_status(0));
 
     assert_that(&paths::root().join("foo"), existing_dir());
@@ -177,12 +177,12 @@ test!(git_prefers_command_line {
     fs::mkdir(&root.join(".cargo"), USER_RWX).unwrap();
     File::create(&root.join(".cargo/config")).write_str(r#"
         [cargo-new]
-        git = false
+        vcs = "none"
         name = "foo"
         email = "bar"
     "#).unwrap();
 
-    assert_that(cargo_process("new").arg("foo").arg("--git").cwd(td.path().clone())
+    assert_that(cargo_process("new").arg("foo").arg("--vcs").arg("git").cwd(td.path().clone())
                                     .env("USER", Some("foo")),
                 execs().with_status(0));
     assert!(td.path().join("foo/.gitignore").exists());